inline
ThreadMutex::ThreadMutex ()
{
#if defined NDK_LINUX
    ::pthread_mutex_init (&this->mutex_, NULL);
#elif defined NDK_WIN32
    //::InitializeCriticalSection(&this->mutex_);
    ::InitializeCriticalSectionAndSpinCount(&this->mutex_, 4000);
#endif
}
inline
ThreadMutex::~ThreadMutex ()
{
#if defined NDK_LINUX
    ::pthread_mutex_destroy (&this->mutex_);
#elif defined NDK_WIN32
    ::DeleteCriticalSection(&this->mutex_);
#endif
}
inline
int ThreadMutex::acquire ()
{
#if defined NDK_LINUX
    int result = ::pthread_mutex_lock (&this->mutex_);
    if (result != 0)
    {
        errno = result;
        return -1;
    }
#elif defined NDK_WIN32
    ::EnterCriticalSection(&this->mutex_);
#endif
    return 0;
}
inline
int ThreadMutex::acquire (const TimeValue *timeout)
{
#if defined NDK_LINUX
    struct timespec ts;
    ts = NDK::gettimeofday () + *timeout;
    int result = ::pthread_mutex_timedlock (&this->mutex_, &ts);
    if (result != 0)
    {
        errno = result;
        return -1;
    }
    return 0;
#elif defined NDK_WIN32
    if (timeout == 0)
        return this->acquire ();
    return -1;
#endif
}
inline
int ThreadMutex::tryacquire ()
{
#if defined NDK_LINUX
    int result = ::pthread_mutex_trylock (&this->mutex_);
    if (result != 0)
    {
        errno = result;
        return -1;
    }
    return 0;
#elif defined NDK_WIN32
    return ::TryEnterCriticalSection(&this->mutex_) ? 0 : -1;
#endif
}
inline
int ThreadMutex::release ()
{
#if defined NDK_LINUX
    int result = ::pthread_mutex_unlock (&this->mutex_);
    if (result != 0)
    {
        errno = result;
        return -1;
    }
#elif defined NDK_WIN32
    ::LeaveCriticalSection(&this->mutex_);
#endif
    return 0;
}
inline
#if defined NDK_LINUX
const pthread_mutex_t &ThreadMutex::lock () const
#elif defined NDK_WIN32
const CRITICAL_SECTION &ThreadMutex::lock () const
#endif
{
    return this->mutex_;
}


